package Sequence;

public class Test1035 {
    public static void main(String[] args) {
        int[] nums1 = {1,4,2};
        int[] nums2={1,2,4};
        System.out.println(new Solution1035_1().maxUncrossedLines(nums1, nums2));
    }
}

class Solution1035_1 {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int length1 = nums1.length;
        int length2 = nums2.length;
        int[][] dp = new int[length1+1][length2+1];

        for (int i = 1; i <= length1; i++) {
            for (int j = 1; j <= length2; j++) {
                if (nums1[i-1] == nums2[j-1]){
                    dp[i][j] = Math.max(dp[i-1][j - 1] + 1, dp[i- 1][j]);
                }else{
                    dp[i][j] = Math.max(dp[i][j-1], dp[i- 1][j]);
                }
            }
        }
        return dp[length1][length2];
    }
}
